// pages/smartlife/post_auto_insurance/post_auto_insurance.js
var common = require('../../../utils/common.js')
var util = require('../../../utils/util.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    autoList: [],
    addHidden:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var appUsed = wx.getStorageSync('appused')
    if (!appUsed) {
      // 跳转到app使用引导页面
      wx.navigateTo({
        url: '/pages/smartlife/guide/guide'
      })
      wx.setStorageSync('appused', true)
    }
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    if (app.globalData.wangcaiToken) {
      // console.log(app.globalData.wangcaiToken)
      // console.log("直接获取")
      that.getData();
    } else {
      // console.log("间接获取")
      //调用应用实例的方法获取全局数据
      app.getToken(function (wangcaiToken) {
        //更新数据
        that.getData();
      })
    }
    
  },
  onShareAppMessage: function () {
    return {
      title: '我们不赚钱，我们只是保险公司的搬运工！',
      path: '/pages/smartlife/post_auto_insurance/post_auto_insurance'
    }
  },
  getData: function () {
    var that = this
    wx.request({
      url: common.postUrl() + 'autoInsuranceList',
      data: {
        token: app.globalData.wangcaiToken
      },
      // header: {}, // 设置请求的 header
      success: function (res) {
        if (res.data.status == 1) {
          that.setData({
            autoList: res.data.data
          })
        } else {
          // app.errorToGetToken()
        }
        wx.hideToast()
      }
    })
  },
  moveStart:function(e) {
    var that = this
    that.setData({
      addHidden:true
    })
  },
  moveEnd:function(e) {
    var that = this
    that.setData({
      addHidden: false
    })
  },
  autoClick: function (e) {
    var that = this
    var status = that.data.autoList[e.currentTarget.dataset.index].state
    if (status == 1) {
      wx.navigateTo({
        url: '/pages/smartlife/post_auto_insurance/insurance_add/insurance_add?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/smartlife/post_auto_insurance/insurance_detail/insurance_detail?id=' + e.currentTarget.dataset.id+'&status='+status,
      })
    }
  },
  addClick:function(e) {
    wx.navigateTo({
      url: '/pages/smartlife/post_auto_insurance/insurance_add/insurance_add',
    })
  }
})